//
//  FunCollectionManager.m
//  LoveTourism
//
//  Created by qianfeng0 on 16/6/24.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "FunCollectionManager.h"
#import <FMDB.h>

@interface FunCollectionManager ()
//数据库
@property (nonatomic,strong)FMDatabase * dataBase;
//数据库沙盒路径
@property (nonatomic,copy)NSString * dbPath;

@end
@implementation FunCollectionManager

+ (instancetype)shareManager {
    static FunCollectionManager * manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[FunCollectionManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dataBase = [[FMDatabase alloc] initWithPath:self.dataBasePath];
        if ([self.dataBase open]) {
            
            NSString *sql = @"create table if not exists fun(pic text not null,title text not null,type text not null,price text not null,team text not null,date text not null,url text not null)";
            NSString *favSql = @"create table if not exists fav(favId text not null,model BLOB NOT NULL)";
            
            if ([self.dataBase executeUpdate:favSql]) {
                NSLog(@"创建收藏表成功");
            } else {
                NSLog(@"创建收藏表失败");
            }
            
            //创建
            if ([self.dataBase executeUpdate:sql]){
                NSLog(@"创建表成功");
            }else{
                NSLog(@"创建表失败");
            }
            [self.dataBase close];
        }
    }
    return self;
}
//创建数据库
- (NSString *)dataBasePath{
    if (!_dbPath) {
        NSString * sanBox = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        _dbPath = [sanBox stringByAppendingPathComponent:@"Fun.sqlite"];
    }
    return _dbPath;
}

/**
 *  插入收藏
 */
- (BOOL)insertIntoFavWith:(NSString *)favId and:(NSData *)model {
    if ([self.dataBase open]) {
        NSString *sql = @"INSERT INTO fav(favId,model) VALUES(?,?)";
        if ([self.dataBase executeUpdate:sql,favId,model]) {
            [self.dataBase close];
            return YES;
        } else {
            [self.dataBase close];
            return NO;
        }
    } else {
        return NO;
    }
}

/**
 *  删除收藏
 */
- (BOOL)deleteFormFavBy:(NSString *)favId {
    if ([self.dataBase open]) {
        NSString *sql = @"DELETE FROM fav WHERE favId = ?";
        if ([self.dataBase executeUpdate:sql,favId]) {
            [self.dataBase close];
            return YES;
        } else {
            [self.dataBase close];
            return NO;
        }
    } else {
        return NO;
    }
}

/**
 *  得到所有收藏
 */
- (NSArray *)getAllFavmodel {
    NSMutableArray *ary = [NSMutableArray new];
    if ([self.dataBase open]) {
        NSString *sql = @"SELECT * FROM fav";
        FMResultSet *set = [self.dataBase executeQuery:sql];
        while ([set next]) {
            NSData *data = [set dataForColumnIndex:1];
            [ary addObject:data];
        }
        [self.dataBase close];
        return ary;
    } else {
        return nil;
    }
}

/**
 *  查询某个收藏是否存在
 */
- (BOOL)favIsExist:(FoodModel *)model {
    if ([self.dataBase open]) {
        NSString *sql = @"SELECT * FROM fav WHERE favId = ?";
        FMResultSet *set = [self.dataBase executeQuery:sql,model.dishes_id];
        if ([set next]) {
            [self.dataBase close];
            return YES;
        } else {
            [self.dataBase close];
            return NO;
        }
    } else {
        return NO;
    }
}

//添加
- (BOOL)addFavorite:(FunModel *)model{
    
    
    BOOL success;
    if ([self.dataBase open]) {
        NSString * sql = @"insert into fun(pic,title,type,price,team,date,url) values(?,?,?,?,?,?,?)";
        if([self.dataBase executeUpdate:sql,model.imgurl,model.subject,model.property,model.price,model.leaderuname,model.timerange,model.eventDetailUrl]){
            success = 1;
            NSLog(@"收藏成功");
        }else{
            NSLog(@"收藏失败");
            success = 0;
        }
        [self.dataBase close];
    }
    return success;
}
//是否已经收藏
- (BOOL)favoriateIsExist:(FunModel *)model{
    BOOL exist = 0;
    if ([self.dataBase open]) {
        NSString * sql = @"select * from fun where title = ?";
        //查询
        FMResultSet * resultSet = [self.dataBase executeQuery:sql,model.subject];
        //遍历
        while ([resultSet next]) {
            //如果能进入循环就说明已经存在
            exist = 1;
        }
    }
    return exist;
}
//查询所有
- (NSMutableArray *)selectAll{
    NSMutableArray * resultArray = [NSMutableArray array];
    //打开数据库
    if ([self.dataBase open]) {
        NSString * sql = @"select * from fun";
        FMResultSet * resultSet = [self.dataBase executeQuery:sql];
        //遍历
        while ([resultSet next]) {
            
            FunModel * model = [[FunModel alloc] init];
            model.leaderuname = [resultSet stringForColumn:@"team"];
            model.price = [resultSet stringForColumn:@"price"];
            model.timerange = [resultSet stringForColumn:@"date"];
            model.property = [resultSet stringForColumn:@"type"];
            model.subject = [resultSet stringForColumn:@"title"];
            model.imgurl = [resultSet stringForColumn:@"pic"];
            model.eventDetailUrl = [resultSet stringForColumn:@"url"];
            [resultArray addObject:model];
        }
        //关
        [self.dataBase close];
    }
    return resultArray;
}

//删除
- (BOOL)deleByName:(FunModel *)model{
    BOOL success = NO;
    NSString * sql = @"delete from fun where title = ?";
    if ([self.dataBase open]) {
        if([self.dataBase executeUpdate:sql,model.subject]){
            success = YES;
        }
        [self.dataBase close];
    }
    return success;
}
@end

















